<?php
namespace Home\Service;

class RedService
{
    /**
     * [payCallback description]
     * @param  [type] $unique         [description]
     * @param  string $transaction_id [description]
     * @param  float  $wx_money       [description]
     * @return [type]                 [description]
     */
    public function payCallback($unique, $transaction_id = '', $wx_money = 0.00)
    {
        if (!$unique) {
            return false;
        }

        $red = M('red_bag')->where(['unique' => $unique])->find();
        if (!$red) {
            return false;
        }

        // 已付款
        if ($red['is_pay']) {
            return false;
        }

        // 微信支付 不一定是支付成功的
        if ($transaction_id && $wx_money) {
            $data = [
                'unpay_money'    => $red['unpay_money'] - $wx_money, // 减少未付款金额

                'transaction_id' => $transaction_id,
                'wx_pay_money'   => $wx_money,/*
                'is_pay'         => 1, // 有微信支付，就必须是支付成功
                'pay_time'       => CUR_DATETIME,*/
            ];

            return $result = M('red_bag')->where(['unique' => $unique])->save($data);
        }

        // 余额支付
        $user_money = M('user')->where(['id' => $red['user_id']])->getField('money');
        // 余额不足
        if ($user_money < $red['balance_pay_money']) {
            return false;
        }

        begin();
        $result1 = D('user', 'Service')->moneyLog($red['user_id'], -$red['balance_pay_money'], '支付红包');

        $result2 = M('red_bag')->where(['unique' => $unique])->setDec('unpay_money', $red['balance_pay_money']);

        $data = [
            'is_pay'   => 1,
            'pay_time' => CUR_DATETIME,
        ];

        $result1 = M('red_bag')->where(['unique' => $unique])->save($data);
        if ($result1 && $result2) {
            commit();
            return true;
        } else {
            rollback();
            return false;
        }
    }

    /**
     * 处理红包余额支付金额，微信支付金额
     * @param  [type] $unique [description]
     * @return [type]         [description]
     */
    public function calculate($unique)
    {
        if (!$unique) {
            return false;
        }

        $red = M('red_bag')->where(['unique' => $unique])->find();
        if (!$red) {
            return fasle;
        }

        // 只处理未支付的红包
        if ($red['status'] != 1) {
            return false;
        }

        $user_id    = $red['user_id'];
        $user_money = M('user')->where(['id' => $user_id])->getField('money');

        // 红包金额
        $money = $red['money'];
        if ($money < setting('red_send_min_money')) {
            return false;
        }

        // 服务费
        $service_charge = $red['service_charge'];
        if ($service_charge != setting('red_service_charge') * $money) {
            return false;
        }

        // 需要支付的总金额
        $total_pay_money = $money + $service_charge;

        // 余额支付多少 微信支付多少
        $balance_pay_money = $user_money > $total_pay_money ? $total_pay_money : $user_money;
        $wx_pay_money      = $total_pay_money - $balance_pay_money;

        // 未付款金额
        $unpay_money = $total_pay_money;

        // 更新信息
        $red_update = [
            'total_pay_money'   => $total_pay_money,
            'unpay_money'       => $unpay_money,

            // 'balance_pay_money' => $balance_pay_money,
            // 'wx_pay_money'      => $wx_pay_money,
        ];
        M('red_bag')->save(array_merge($red, $red_update));
    }

    public function pay($unique)
    {
        if (!$unique) {
            return false;
        }

        // 初始化数据
        $this->calculate($unique);

        $red = M('red_bag')->where(['unique' => $unique])->find();
        if (!$red) {
            return fasle;
        }
        
        $result = $this->payCallback($unique);
        return $result;
    }
}
